package com.ynet.middleground.user.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ynet.cache.redis.IfpRedisson;
import com.ynet.core.common.BaseReqObj;
import com.ynet.core.log.IfpLogger;
import com.ynet.host.client.HostSenderService;
import com.ynet.host.http.HttpRequest;
import com.ynet.host.http.HttpResponse;
import com.ynet.middleground.user.bean.CloudPlatformMerchantReq;
import com.ynet.middleground.user.bean.CloudPlatformUserReq;
import com.ynet.middleground.user.bean.CloudPlatfromCitySearchReq;
import com.ynet.middleground.user.dao.CloudPlatformLogMapper;
import com.ynet.middleground.user.dao.CloudPlatformSyncMapper;
import com.ynet.middleground.user.dto.CloudPlatformCityDTO;
import com.ynet.middleground.user.dto.CloudPlatformProvinceDTO;
import com.ynet.middleground.user.entity.CloudPlatformLog;
import com.ynet.middleground.user.entity.CloudPlatformSync;
import com.ynet.middleground.user.external.cloudPlatform.CloudPlatformConfig;
import com.ynet.middleground.user.external.cloudPlatform.CloudPlatformUri;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liwq
 * @description
 * @date 2021/7/2 17:28
 */
@Component
public class CloudPlatformUtil {

    @Autowired
    private HostSenderService hostSenderService;

    @Autowired
    private IfpRedisson redisson;

    @Autowired
    private CloudPlatformLogMapper cloudPlatformLogMapper;

    @Autowired
    private CloudPlatformConfig cloudPlatformConfig;

    @Autowired
    CloudPlatformSyncMapper cloudPlatformSyncMapper;

    /**
     * redis 全局日志记录开关，决定是否将调用日志入库
     */
    public static String REDIS_KEY = "ifp:system:settings:logswitch";

    /**
     * 向 api 服务发送请求
     * 
     * @param callSide 调用方(0:中台；1：云平台)
     * @param headMap 请求头参数
     * @param params 请求信息参数
     * @param url 请求完整地址
     * @param httpMethod 请求方式
     * @param reqObj 基础请求对象
     * @return
     */
    public JSONObject sendApiWithLog(Integer callSide, Map<String, String> headMap, Map<String, String> params,
        String url, String httpMethod, BaseReqObj reqObj) {
        // params 不能为空
        if (params == null || params.size() == 0) {
            params = new HashMap<>(16);
            params.put("clientId", cloudPlatformConfig.getClientId());
        }
        // 方法开始执行的时间
        LocalDateTime startTime = LocalDateTime.now();
        HttpRequest request = new HttpRequest();
        request.setUrl(url);
        request.setMethod(httpMethod);
        request.setMessage(params);
        request.setParams(params);
        if ("GET".equalsIgnoreCase(httpMethod)) {
            Map<String, String> getParams = new HashMap<>(16);
            params.forEach((k, v) -> {
                getParams.put(k, String.valueOf(v));
            });
            request.setParams(getParams);
        }
        request.setHeader(headMap);
        JSONObject apiResult;
        Object json = JSONObject.toJSON(request);
        IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(),
            " 开始 " + LocalDate.now() + "请求地址" + url);
        IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(),
            " 开始 " + LocalDate.now() + "请求参数：" + json, request);
        HttpResponse response = (HttpResponse)hostSenderService.request("id_http_x", request, JSONObject.class);

        Object responseJson = JSONObject.toJSON(response);
        IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(),
            " 结束 " + LocalDate.now() + "响应报文：" + responseJson, response);
        // 这是响应的头信息
        // 这是响应的 http 状态码，只有合法的状态才会返回，其它状态码会抛异常
        // 200 为默认的合法状态码，如有需求，可在 yml 中可自定义其它状态码
        IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(),
            " 请求 api 服务, api 地址: {}， 响应状态码，status: {}", url, response.getStatus());
        // 这是响应的报文体反格式化之后的对象
        apiResult = (JSONObject)response.getMessage();

        // 调用日志入库
        String o = redisson.get(REDIS_KEY);
        if (o == null || "1".equals(o)) {
            CloudPlatformLog cloudPlatformLog = new CloudPlatformLog();
            cloudPlatformLog.setCallSide(callSide);
            cloudPlatformLog.setCallInterface(url);
            cloudPlatformLog.setRequestMsg(params.toString());
            cloudPlatformLog.setCreateBy(reqObj.getOperationUserId());
            cloudPlatformLog.setCreationDate(startTime);
            cloudPlatformLog.setResponseMsg(apiResult == null ? null : apiResult.toString());
            cloudPlatformLog.setModifiedBy(reqObj.getOperationUserId());
            cloudPlatformLog.setGmtModified(LocalDateTime.now());
            cloudPlatformLog.setStatus(response.getStatus() == 200 ? 1 : 0);
            cloudPlatformLogMapper.insert(cloudPlatformLog);
        }
        return apiResult;
    }

    /**
     * 向 api 服务发送请求
     *
     * @param callSide 调用方(0:中台；1：云平台)
     * @param headMap 请求头参数
     * @param params 请求信息参数
     * @param url 请求完整地址
     * @param httpMethod 请求方式
     * @param reqObj 基础请求对象
     * @return
     */
    public JSONArray sendApiWithLogJsonArray(Integer callSide, Map<String, String> headMap, Map<String, String> params,
        String url, String httpMethod, BaseReqObj reqObj) {
        // params 不能为空
        if (params == null || params.size() == 0) {
            params = new HashMap<>(16);
            params.put("clientId", cloudPlatformConfig.getClientId());
        }
        // 方法开始执行的时间
        LocalDateTime startTime = LocalDateTime.now();
        HttpRequest request = new HttpRequest();
        request.setUrl(url);
        request.setMethod(httpMethod);
        request.setMessage(params);
        request.setParams(params);
        if ("GET".equalsIgnoreCase(httpMethod)) {
            Map<String, String> getParams = new HashMap<>(16);
            params.forEach((k, v) -> {
                getParams.put(k, String.valueOf(v));
            });
            request.setParams(getParams);
        }
        request.setHeader(headMap);
        JSONArray apiResult;
        Object json = JSONObject.toJSON(request);
        IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(),
            " 开始 " + LocalDate.now() + "请求地址" + url);
        IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(),
            " 开始 " + LocalDate.now() + "请求参数：" + json, request);
        HttpResponse response = (HttpResponse)hostSenderService.request("id_http_x", request, JSONArray.class);

        Object responseJson = JSONArray.toJSON(response);
        IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(),
            " 结束 " + LocalDate.now() + "响应报文：" + responseJson, response);
        // 这是响应的头信息
        // 这是响应的 http 状态码，只有合法的状态才会返回，其它状态码会抛异常
        // 200 为默认的合法状态码，如有需求，可在 yml 中可自定义其它状态码
        IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(),
            " 请求 api 服务, api 地址: {}， 响应状态码，status: {}", url, response.getStatus());
        // 这是响应的报文体反格式化之后的对象
        apiResult = (JSONArray)response.getMessage();

        // 调用日志入库
        String o = redisson.get(REDIS_KEY);
        if (o == null || "1".equals(o)) {
            CloudPlatformLog cloudPlatformLog = new CloudPlatformLog();
            cloudPlatformLog.setCallSide(callSide);
            cloudPlatformLog.setCallInterface(url);
            cloudPlatformLog.setRequestMsg(params.toString());
            cloudPlatformLog.setCreateBy(reqObj.getOperationUserId());
            cloudPlatformLog.setCreationDate(startTime);
            cloudPlatformLog.setResponseMsg(apiResult == null ? null : apiResult.toString());
            cloudPlatformLog.setModifiedBy(reqObj.getOperationUserId());
            cloudPlatformLog.setGmtModified(LocalDateTime.now());
            cloudPlatformLog.setStatus(response.getStatus() == 200 ? 1 : 0);
            cloudPlatformLogMapper.insert(cloudPlatformLog);
        }
        return apiResult;
    }

    /**
     * 获取token
     *
     * @param baseReqObj
     * @return
     */
    public String getMd5Token(BaseReqObj baseReqObj) {
        String md5Token = null;
        // 获取加密种子
        JSONObject jsonObject = this.sendApiWithLog(0, null, null,
            cloudPlatformConfig.getApiHost() + CloudPlatformUri.getEncryptionSeed, "POST", baseReqObj);
        String seedStr = jsonObject.getString("seedStr");
        // 获取token
        String token = cloudPlatformConfig.getToken();
        // md5(token+加密种子)
        if (StringUtils.isNotEmpty(seedStr) && StringUtils.isNotEmpty(token)) {
            md5Token = getMd5Str(token + seedStr);
        }
        return md5Token;
    }

    /**
     * 用户数据拉取(根据手机号);返回用户主数据ID+用户详细信息ID 例：867367543512236032+56736754351223698
     * 
     * @return
     */
    public String getUserInfo(BaseReqObj baseReqObj, String mobile, String md5Token) {
        String userId = null;
        String userInfoId = null;
        // 根据手机号拉取用户数据
        Map<String, String> params = new HashMap<>();
        params.put("token", md5Token);
        params.put("client_id", cloudPlatformConfig.getClientId());
        params.put("mobile", mobile);
        JSONObject jsonObject = this.sendApiWithLog(0, null, params,
            cloudPlatformConfig.getApiHost() + CloudPlatformUri.getUserInfo, "POST", baseReqObj);
        if (jsonObject == null) {
            return null;
        }
        String status = jsonObject.getString("status");
        // 返回状态：0为正常， 1为异常
        if (StringUtils.isNotEmpty(status) && "0".equals(status)) {
            JSONObject data = jsonObject.getJSONObject("data");
            if (data != null) {
                JSONObject userInfo = data.getJSONObject("userInfo");
                if (userInfo != null) {
                    userId = userInfo.getString("userId");
                    userInfoId = userInfo.getString("userInfoId");
                }
            }
        }
        if (!StringUtils.isEmpty(userId) && !StringUtils.isEmpty(userInfoId)) {
            return userId + "+" + userInfoId;
        }
        return null;
    }

    /**
     * 用户数据拉取(根据手机号);返回用户主数据ID+用户详细信息ID 例：867367543512236032+56736754351223698
     *
     * @return
     */
    public JSONObject getUserInfoSync(BaseReqObj baseReqObj, String mobile, String md5Token) {
        String userId = null;
        String userInfoId = null;
        // 根据手机号拉取用户数据
        Map<String, String> params = new HashMap<>();
        params.put("token", md5Token);
        params.put("client_id", cloudPlatformConfig.getClientId());
        params.put("mobile", mobile);
        JSONObject jsonObject = this.sendApiWithLog(0, null, params,
            cloudPlatformConfig.getApiHost() + CloudPlatformUri.getUserInfo, "POST", baseReqObj);
        if (jsonObject == null) {
            return null;
        }
        return jsonObject;
    }

    /**
     * 客商数据拉取(企业社会信用代码/个人身份证号)
     *
     * @return
     */
    public String getCompanyInfo(BaseReqObj baseReqObj, String social_no, String md5Token) {
        String companyId = null;
        // 根据（企业社会信用代码/个人身份证号）拉取客商数据
        Map<String, String> params = new HashMap<>();
        params.put("token", md5Token);
        params.put("client_id", cloudPlatformConfig.getClientId());
        params.put("social_no", social_no);
        JSONObject jsonObject = this.sendApiWithLog(0, null, params,
            cloudPlatformConfig.getApiHost() + CloudPlatformUri.getCompanyInfo, "POST", baseReqObj);
        if (jsonObject == null) {
            return null;
        }
        String status = jsonObject.getString("status");
        // 返回状态：0为正常， 1为异常
        if (StringUtils.isNotEmpty(status) && "0".equals(status)) {
            JSONObject data = jsonObject.getJSONObject("data");
            if (data != null) {
                JSONObject userInfo = data.getJSONObject("companyInfo");
                if (userInfo != null) {
                    companyId = userInfo.getString("companyId");
                }
            }
        }
        return companyId;
    }

    /**
     * 客商数据拉取跑批(企业社会信用代码/个人身份证号)
     *
     * @return
     */
    public JSONObject getCompanyInfoSync(BaseReqObj baseReqObj, String social_no, String md5Token) {
        String companyId = null;
        // 根据（企业社会信用代码/个人身份证号）拉取客商数据
        Map<String, String> params = new HashMap<>();
        params.put("token", md5Token);
        params.put("client_id", cloudPlatformConfig.getClientId());
        params.put("social_no", social_no);
        JSONObject jsonObject = this.sendApiWithLog(0, null, params,
            cloudPlatformConfig.getApiHost() + CloudPlatformUri.getCompanyInfo, "POST", baseReqObj);
        if (jsonObject == null) {
            return null;
        }
        return jsonObject;
    }

    /**
     * 用户注册||认证||修改;(返回用户主数据ID+用户详细信息ID 例：867367543512236032+56736754351223698)
     * 
     * @return
     */
    public String commitUser(CloudPlatformUserReq c, String md5Token, BaseReqObj baseReqObj, String operation) {
        Map<String, Object> dadaValue = new HashMap<>(16);
        // 注册
        if ("0".equals(c.getOperateFlag())) {
            dadaValue.put("userTel", c.getUserTel());
            // dadaValue.put("password", c.getPassword());
            dadaValue.put("email", c.getEmail());
            // dadaValue.put("isAuth", "1");
        }
        // 认证
        if ("1".equals(c.getOperateFlag())) {
            dadaValue.put("userId", c.getUserId());
            dadaValue.put("userName", c.getUserName());
            dadaValue.put("realName", c.getUserName());
            dadaValue.put("userCardNo", c.getUserCardNo());
            dadaValue.put("client_id", cloudPlatformConfig.getClientId());
            dadaValue.put("email", c.getEmail());
            dadaValue.put("isAuth", "1");
        }
        // 修改
        if ("2".equals(c.getOperateFlag())) {
            dadaValue.put("userId", c.getUserId());
            dadaValue.put("userName", c.getUserName());
            dadaValue.put("userTel", c.getUserTel());
            dadaValue.put("realName", c.getUserName());
            dadaValue.put("userCardNo", c.getUserCardNo());
            // dadaValue.put("password", c.getPassword());
            dadaValue.put("client_id", cloudPlatformConfig.getClientId());
            dadaValue.put("email", c.getEmail());
            dadaValue.put("isAuth", "1");
        }

        Map<String, Object> userJsonStrValue = new HashMap<String, Object>(16);
        userJsonStrValue.put("client_id", cloudPlatformConfig.getClientId());
        userJsonStrValue.put("data", dadaValue);
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("token", md5Token);
        requestParams.put("userJsonStr", JSON.toJSONString(userJsonStrValue));

        JSONObject jsonObject = this.sendApiWithLog(0, null, requestParams,
            cloudPlatformConfig.getApiHost() + CloudPlatformUri.commitUser, "POST", baseReqObj);
        if (jsonObject == null) {
            return null;
        }
        // 跑批时不记录
        if (StringUtils.isBlank(operation)) {
            String status = jsonObject.getString("status");
            String data = jsonObject.getString("data");
            if (!"0".equals(status)) {
                // 插入异常信息表数据
                addErrorData(baseReqObj.getChannel(), c.getMiddleGroundUserId(), "3", baseReqObj.getOperationUserId(),
                    status, data);
            }
        }
        String userId = jsonObject.getString("user_id");
        String userInfoId = jsonObject.getString("user_info_id");
        if (!StringUtils.isEmpty(userId) && !StringUtils.isEmpty(userInfoId)) {
            return userId + "+" + userInfoId;
        }
        return null;
    }

    /**
     * 客商新增||修改
     *
     * @return
     */
    public String commitMerchant(CloudPlatformMerchantReq c, String md5Token, BaseReqObj baseReqObj, String operation) {
        Map<String, Object> dadaValue = new HashMap<>();
        // 0 个人
        if (0 == c.getCustomerType()) {
            // 0 新增
            if ("0".equals(c.getOperateFlag())) {
                dadaValue.put("customerType", "0");
                dadaValue.put("companyName", c.getCompanyName());
                dadaValue.put("uniformSocialCreditCode", c.getUniformSocialCreditCode());
                dadaValue.put("companyTel", c.getCompanyTel());
                dadaValue.put("isAuth", "1");
            }
            // 1 修改
            if ("1".equals(c.getOperateFlag())) {
                dadaValue.put("companyId", c.getCompanyId());
                dadaValue.put("customerType", "0");
                dadaValue.put("companyName", c.getCompanyName());
                dadaValue.put("uniformSocialCreditCode", c.getUniformSocialCreditCode());
                dadaValue.put("companyTel", c.getCompanyTel());
                dadaValue.put("isAuth", "1");
            }

        }
        // 1 企业
        if (1 == c.getCustomerType()) {
            // 0 新增
            if ("0".equals(c.getOperateFlag())) {
                dadaValue.put("customerType", "1");
                dadaValue.put("companyName", c.getCompanyName());
                dadaValue.put("industryType", c.getIndustryType());
                dadaValue.put("province", c.getProvince());
                dadaValue.put("city", c.getCity());
                dadaValue.put("address", c.getAddress());
                //dadaValue.put("businessType", c.getBusinessType());
                dadaValue.put("legalPerson", c.getLegalPerson());
                dadaValue.put("legalPersonCardNo", c.getLegalPersonCardNo());
                //dadaValue.put("businessState", c.getBusinessState());
                dadaValue.put("businessScope", c.getBusinessScope());
                dadaValue.put("companyTel", c.getCompanyTel());
                dadaValue.put("businessAddress", c.getBusinessAddress());
                dadaValue.put("licenseImgUrl", c.getLicenseImgUrl());
                if (StringUtils.isNotBlank(c.getOrganizationCode())) {
                    dadaValue.put("organizationCode", c.getOrganizationCode());
                } else {
                    dadaValue.put("uniformSocialCreditCode", c.getUniformSocialCreditCode());
                }
                dadaValue.put("isAuth", "1");
            }
            // 1 修改
            if ("1".equals(c.getOperateFlag())) {
                dadaValue.put("companyId", c.getCompanyId());
                dadaValue.put("customerType", "1");
                dadaValue.put("companyName", c.getCompanyName());
                dadaValue.put("industryType", c.getIndustryType());
                dadaValue.put("province", c.getProvince());
                dadaValue.put("city", c.getCity());
                dadaValue.put("address", c.getAddress());
                //dadaValue.put("businessType", c.getBusinessType());
                dadaValue.put("legalPerson", c.getLegalPerson());
                dadaValue.put("legalPersonCardNo", c.getLegalPersonCardNo());
                //dadaValue.put("businessState", c.getBusinessState());
                dadaValue.put("businessScope", c.getBusinessScope());
                dadaValue.put("companyTel", c.getCompanyTel());
                dadaValue.put("businessAddress", c.getBusinessAddress());
                dadaValue.put("licenseImgUrl", c.getLicenseImgUrl());
                if (StringUtils.isNotBlank(c.getOrganizationCode())) {
                    dadaValue.put("organizationCode", c.getOrganizationCode());
                } else {
                    dadaValue.put("uniformSocialCreditCode", c.getUniformSocialCreditCode());
                }
                dadaValue.put("isAuth", "1");
            }

        }
        dadaValue.put("isAuth", "1");
        Map<String, Object> userJsonStrValue = new HashMap<>();
        userJsonStrValue.put("client_id", cloudPlatformConfig.getClientId());
        userJsonStrValue.put("data", dadaValue);
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("token", md5Token);
        requestParams.put("companyStr", JSON.toJSONString(userJsonStrValue));

        JSONObject jsonObject = this.sendApiWithLog(0, null, requestParams,
            cloudPlatformConfig.getApiHost() + CloudPlatformUri.commitCompany, "POST", baseReqObj);
        if (jsonObject == null) {
            return null;
        }
        // 跑批时不记录
        if (StringUtils.isBlank(operation)) {
            String status = jsonObject.getString("status");
            String data = jsonObject.getString("data");
            if (!"0".equals(status)) {
                // 插入异常信息表数据
                addErrorData(baseReqObj.getChannel(), c.getMiddleGroundUserId(), 0 == c.getCustomerType() ? "2" : "0",
                    baseReqObj.getOperationUserId(), status, data);
            }
        }
        String userId = jsonObject.getString("company_id");
        return userId;
    }

    /**
     * 将异常数据记录到错误表
     * 
     * @param channel
     * @param userId
     * @param userType
     * @param operationUserId
     * @param errorCode
     * @param errorMsg
     */
    public void addErrorData(String channel, Integer userId, String userType, Integer operationUserId, String errorCode,
        String errorMsg) {
        CloudPlatformSync cloudPlatformSync = new CloudPlatformSync();
        cloudPlatformSync.setUserId(userId);
        // 中台用户类型：0-企业用户，2-个人用户，3企业员工用户
        cloudPlatformSync.setUserType(userType);
        cloudPlatformSync.setChannel(channel);
        cloudPlatformSync.setStatus("0");
        cloudPlatformSync.setErrorCode(errorCode);
        cloudPlatformSync.setErrorMsg(errorMsg);
        cloudPlatformSync.setGmtCreate(LocalDateTime.now());
        cloudPlatformSync.setCreateBy(operationUserId);
        cloudPlatformSyncMapper.insert(cloudPlatformSync);
    }

    /**
     * 云平台md5加密
     *
     * @param str
     */
    public static String getMd5Str(String str) {
        String result = null;
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            md5.update((str).getBytes("UTF-8"));
            byte b[] = md5.digest();

            int i;
            StringBuffer buf = new StringBuffer("");

            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
                result = buf.toString();
            }

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public List<CloudPlatformProvinceDTO> searchAllProvince(BaseReqObj baseReqObj) {
        Map<String, String> requestParams = new HashMap<>();
        JSONArray jSONArray = this.sendApiWithLogJsonArray(0, null, requestParams,
            cloudPlatformConfig.getApiHost() + CloudPlatformUri.getProvinceInfo, "POST", baseReqObj);
        if (jSONArray == null) {
            return null;
        }
        return jSONArray.toJavaList(CloudPlatformProvinceDTO.class);
    }

    public List<CloudPlatformCityDTO> searchCityByProvinceCode(CloudPlatfromCitySearchReq reqObj) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("provinceCode", reqObj.getProvinceCode());
        JSONArray jSONArray = this.sendApiWithLogJsonArray(0, null, requestParams,
            cloudPlatformConfig.getApiHost() + CloudPlatformUri.getCityInfo, "POST", reqObj);
        if (jSONArray == null) {
            return null;
        }
        return jSONArray.toJavaList(CloudPlatformCityDTO.class);
    }


    /**
     * 跑批客商数据id拉取(企业社会信用代码/个人身份证号)
     *
     * @return
     */
    public String getCompanyInfoId(BaseReqObj baseReqObj, String social_no, String md5Token) {
        String companyId = null;
        // 根据（企业社会信用代码/个人身份证号）拉取客商数据
        Map<String, String> params = new HashMap<>();
        params.put("token", md5Token);
        params.put("client_id", cloudPlatformConfig.getClientId());
        params.put("social_no", social_no);
        JSONObject jsonObject = this.sendApiWithLog(0, null, params,
                cloudPlatformConfig.getApiHost() + CloudPlatformUri.getCompanyInfoId, "POST", baseReqObj);
        if (jsonObject == null) {
            return null;
        }
        String status = jsonObject.getString("status");
        // 返回状态：0为正常， 1为异常
        if (StringUtils.isNotEmpty(status) && "0".equals(status)) {
            JSONObject data = jsonObject.getJSONObject("data");
            if (data != null) {
                JSONObject userInfo = data.getJSONObject("companyInfo");
                if (userInfo != null) {
                    companyId = userInfo.getString("companyId");
                }
            }
        }
        return companyId;
    }
}
